﻿
using Xacml.Core;
using System;
using System.Collections;
using System.Xml;

namespace Xacml.Core.Context
{
  public class RequestElementReadWrite : XacmlElement
  {
    private SubjectCollectionReadWrite _subjects = new SubjectCollectionReadWrite();
    private ResourceCollectionReadWrite _resources = new ResourceCollectionReadWrite();
    private ActionElementReadWrite _action;
    private EnvironmentElementReadWrite _environment;

    public virtual SubjectCollectionReadWrite Subjects
    {
      get
      {
        return this._subjects;
      }
      set
      {
        this._subjects = value;
      }
    }

    public virtual ResourceCollectionReadWrite Resources
    {
      get
      {
        return this._resources;
      }
      set
      {
        this._resources = value;
      }
    }

    public virtual ActionElementReadWrite Action
    {
      get
      {
        return this._action;
      }
      set
      {
        this._action = value;
      }
    }

    public virtual EnvironmentElementReadWrite Environment
    {
      get
      {
        return this._environment;
      }
      set
      {
        this._environment = value;
      }
    }

    public override bool IsReadOnly
    {
      get
      {
        return false;
      }
    }

    public RequestElementReadWrite(SubjectCollectionReadWrite subjects, ResourceCollectionReadWrite resources, ActionElementReadWrite action, EnvironmentElementReadWrite environment, XacmlVersion schemaVersion)
      : base(XacmlSchema.Context, schemaVersion)
    {
      this._subjects = subjects;
      this._resources = resources;
      this._action = action;
      this._environment = environment;
    }

    public RequestElementReadWrite(XmlReader reader, XacmlVersion schemaVersion)
      : base(XacmlSchema.Context, schemaVersion)
    {
      if (reader.LocalName == "Request")
      {
        while (reader.Read())
        {
          switch (reader.LocalName)
          {
            case "Subject":
              this._subjects.Add(new SubjectElementReadWrite(reader, schemaVersion));
              continue;
            case "Resource":
              this._resources.Add(new ResourceElementReadWrite(reader, schemaVersion));
              continue;
            case "Action":
              this._action = new ActionElementReadWrite(reader, schemaVersion);
              continue;
            case "Environment":
              this._environment = new EnvironmentElementReadWrite(reader, schemaVersion);
              continue;
            default:
              continue;
          }
        }
      }
      else
        throw new Exception(Resource.ResourceManager[Resource.MessageKey.exc_invalid_node_name, new object[1]
        {
          (object) reader.LocalName
        }]);
    }

    public void WriteDocument(XmlWriter writer, Hashtable namespaces)
    {
      writer.WriteStartDocument();
      writer.WriteStartElement("Request", string.Empty);
      foreach (DictionaryEntry dictionaryEntry in namespaces)
        writer.WriteAttributeString("xmlns", dictionaryEntry.Key.ToString(), (string) null, dictionaryEntry.Value.ToString());
      if (this._subjects != null)
        this._subjects.WriteDocument(writer);
      if (this._resources != null)
        this._resources.WriteDocument(writer);
      if (this._action != null)
        this._action.WriteDocument(writer);
      if (this._environment != null)
        this._environment.WriteDocument(writer);
      writer.WriteEndElement();
      writer.WriteEndDocument();
    }
  }
}
